#!/usr/bin/env python3
"""
自动查找 PyQt5 插件路径
"""
import os
import sys
import site
from pathlib import Path
from PyQt5.QtWidgets import QApplication, QLabel, QPushButton, QVBoxLayout, QWidget

def find_qt_plugins():
    """自动查找 Qt 插件路径"""
    # 可能的插件路径
    possible_paths = [
        # Conda 环境路径
        Path(sys.prefix) / "Lib" / "site-packages" / "PyQt5" / "Qt5" / "plugins",
        Path(sys.prefix) / "Library" / "plugins",

        # Site-packages 路径
        Path(site.getsitepackages()[0]) / "PyQt5" / "Qt5" / "plugins",

        # 其他常见路径
        Path(os.environ.get('CONDA_PREFIX', '')) / "Lib" / "site-packages" / "PyQt5" / "Qt5" / "plugins",
        ]

    for path in possible_paths:
        if path.exists() and (path / "platforms").exists():
            print(f"找到插件路径: {path}")
            return str(path)

    print("警告: 未找到 Qt 插件路径，尝试默认路径")
    return ""

# 设置插件路径
plugin_path = find_qt_plugins()
if plugin_path:
    os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path

app = QApplication(sys.argv)

window = QWidget()
window.setWindowTitle('PyQt 最小 demo')

layout = QVBoxLayout()
layout.addWidget(QLabel('Hello PyQt'))
btn = QPushButton('点我')
btn.clicked.connect(lambda: print('按钮被点击了!'))
layout.addWidget(btn)

window.setLayout(layout)
window.show()
sys.exit(app.exec_())